import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

//Connection 常用API详解
public class jdbc_Connection {
    //jdbc快速入门
    public static void main(String[] args) throws Exception {
        //1.注册驱动
//        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url="jdbc:mysql:///db1";
        String username="root";
        String password="123456";
        Connection conn = DriverManager.getConnection(url, username, password);
        //3.定义sql语句
        String sql1 = "update account set money = 3000 where id = 1";
        String sql2 = "update account set money = 3000 where id = 2";
        //4.获取执行sql的对象
        Statement stmt = conn.createStatement();
        //开启事务
        try {
            conn.setAutoCommit(false);
            int count1 = stmt.executeUpdate(sql1);//受影响的行数
            System.out.println(count1);
//            int i = 3/0;
            int count2 = stmt.executeUpdate(sql2);//受影响的行数
            System.out.println(count2);
            //提交事务
            conn.commit();
        } catch (SQLException e) {
            //回滚事务
            conn.rollback();
            throw new RuntimeException(e);
        }
        stmt.close();
        conn.close();

    }
}
